package com.heatup.apt.common.util;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * 图片上传到OSS
 *
 * @author: Blank
 * @create: 2016-12-13-下午 4:10
 */
public class ImgUploadToOSSUtils {
    public static final Logger log = LoggerFactory.getLogger(ImgUploadToOSSUtils.class);

    /**
     * 图片上传
     * @param file 要上传的图片
     */
    public static String upload(File file) throws IOException {
        try {
            if (file.exists()) {

                String fileExt = file.getName().substring(file.getName().lastIndexOf("."));
                String imagePath = OssFileUtil.uploadFile(file.getPath(), UUID.randomUUID().toString().substring(0, 8) + fileExt);
                if(StringUtils.isNotBlank(imagePath)){
                    file.delete();
                }
                return imagePath;
            }
        } catch (Exception ex) {
            log.error("ImgUploadToOSSUtils upload img method error! " + ex.getMessage() + ex.toString() + ex);
        }
        return null;
    }
}
